<template>
	<div id='cheat-manage'>
		<div class="breadcrumb">
			<img src="../../../../static/img/outClass_icon_1.png" class="head_img">
			<span class="title">作弊管理</span>
			<div class="fr">
				状态：{{isOpen?"已开启":"已关闭"}}
				<el-button type="primary" @click="operat(0)" v-if='!isOpen' style="margin-left:10px;">开启功能</el-button>
				<el-button type="primary" @click="operat(1)" v-if="isOpen" style="margin-left:10px;">关闭功能</el-button>
			</div>
		</div>

		<div class="search">
			<el-select v-model="semester">
				<el-option v-for='item in semesters' :label='item.semesterName' :value='item.sid' :key="item.sid">
				</el-option>
			</el-select>
			<el-select v-model="teacher" placeholder="全部老师" @change="getClasses">
				<el-option v-for="item in teacherLists" :label="item.name" :value="item.id" :key="item.id"></el-option>
			</el-select>
			<el-select v-model="classes" placeholder="全部课程">
				<el-option v-for="item in classesLists" :label="item.name" :value="item.id" :key="item.id"></el-option>
			</el-select>
			<el-button class="sel_btn" type="primary" @click="getResult">筛选</el-button>
		</div>

		<div class="list">
			<div class="list_head">
				<el-input class="inp" placeholder="请输入学号或姓名" icon="search" v-model="condition" @keyup.enter.native="getResult"
				 :on-icon-click="getResult"></el-input>
				<el-button type="primary" class="import1" @click="getImportUrl">导出</el-button>
			</div>
			<el-table :data="list" @row-click="toDetail">
				<el-table-column prop="campusId" label="学号">
				</el-table-column>
				<el-table-column prop="name" label="姓名">
				</el-table-column>
				<el-table-column prop="teacherName" label="老师">
				</el-table-column>
				<el-table-column prop="className" label="课程">
				</el-table-column>
				<el-table-column prop="cheatNum" label="作弊次数">
				</el-table-column>
			</el-table>
			<div class="pagination">
				<el-pagination v-show="this.totalCount > 0" layout="prev, pager, next, jumper" :page-size="10" :current-page="currentPage"
				 :total="totalCount" @current-change="handleCurrentChange">
				</el-pagination>
			</div>
		</div>
	</div>
	</div>
</template>

<script>
	import { mapState, mapActions } from "vuex";

	export default {
		data() {
			return {
				list: [],
				isOpen: true,
				semesters: [],
				totalCount: 0,
				condition: "",
				isDisabled: true,
				teacherLists: [{ id: 0, name: "全部老师" }],
				classesLists: [{ id: 0, name: "全部课程" }],
				teacher: parseInt(this.$util.getUrlParam("teacher")) || 0,
				classes: parseInt(this.$util.getUrlParam("classes")) || 0,
				semester: parseInt(this.$util.getUrlParam("semester")) || '',
				currentPage: parseInt(this.$util.getUrlParam("pageNum")) || 1
			};
		},
		mounted() {
			//查询作弊黑榜功能的状态；
			this.getActionStatus();
			if (this.user.type == 3) {
				this.teacher = this.user.mid;
			} else {
				this.getTeachers();
			}
			if (this.teacher && this.teacher != 0) {
				this.getClasses();
			}
			this.getSemesters();
			if (this.semester) {
				this.getResult();
			}

		},
		computed: mapState({ user: state => state.user }),
		methods: {
			//学期列表
			getSemesters() {
				let url = this.$util.domain + "/campusadminapi/v1/tools/semesters";

				this.$http({
					url,
					method: "GET"
				}).then(response => {
					if (response.data.error == 10000) {
						let semesterList = response.data.data;
						if (semesterList && semesterList.length > 0) {

							let sid = this.$util.getUrlParam("semester");
							if (sid) {
								this.condition = this.$util.getUrlParam("condition");
							} else {
								this.semester = semesterList[0].sid;
							}

							for (let i = 0; i < semesterList.length; i++) {
								this.semesters.push({
									sid: semesterList[i].sid,
									unids: semesterList[i].unids,
									endDate: semesterList[i].endDate,
									startDate: semesterList[i].startDate,
									semesterName: semesterList[i].semesterName
								});
							}
						}
						this.getResult()
					}
				});
			},
			// 获取老师列表
			getTeachers() {
				let url = this.$util.domain + "/campusadminapi/v1/tools/teachers";
				this.teacherLists = [{ id: 0, name: "全部老师" }];

				this.$http({
					url,
					method: "GET"
				}).then(response => {
					if (response.data.error == 10000) {
						// 老师列表
						let teacherList = response.data.data;
						if (teacherList && teacherList.length > 0) {
							if (this.$util.getUrlParam("teacher")) {
								this.teacher = Number(this.$util.getUrlParam("teacher"));
							}
							for (let i = 0; i < teacherList.length; i++) {
								this.teacherLists.push({
									id: teacherList[i].mid,
									name: teacherList[i].teacherName
								});
							}
						} else {
							this.teacherLists = [{ id: 0, name: "全部老师" }];
						}
					}
				});
			},
			// 获取课程列表
			getClasses() {
				let url = this.$util.domain + "/campusadminapi/v1/tools/classes";
				this.classesLists = [{ id: 0, name: "全部课程" }];
				if (!this.teacher || this.teacher == 0) {
					return;
				}

				this.$http({
					url,
					method: "GET",
					params: {
						sid: this.semester,
						mid: this.teacher
					}
				}).then(response => {
					if (response.data.error == 10000) {
						let dataList = response.data.data;
						if (dataList && dataList.length > 0) {
							for (let i = 0; i < dataList.length; i++) {
								this.classesLists.push({
									name: dataList[i].name,
									id: dataList[i].id
								});
							}
						}
					}
				});
			},
			//获取作弊列表
			getResult() {
				this.list = [];
				let url = this.$util.domain + "/campusadminapi/v1/cheat/search";

				this.$http({
					url,
					method: "POST",
					body: JSON.stringify({
						pageSize: 10,
						pageNum: this.currentPage,
						sid: parseInt(this.semester),
						mid: parseInt(this.teacher),
						cid: parseInt(this.classes),
						keyword: this.condition.trimLeft()
					})
				}).then(response => {
					if (response.data.error == 10000) {
						let cheatList = response.data.data.cheatList;
						this.totalCount = response.data.data.totalCount;
						this.list = [];
						if (cheatList && cheatList.length > 0) {
							for (let i = 0; i < cheatList.length; i++) {
								let $campusId = cheatList[i].campusId,
									$name = cheatList[i].name,
									$teacherName = cheatList[i].teacherName,
									$className = cheatList[i].className,
									$cheatNum = cheatList[i].cheatNum,
									$cheatId = cheatList[i].cheatId;

								this.list.push({
									name: $name,
									cheatId: $cheatId,
									cheatNum: $cheatNum,
									campusId: $campusId,
									className: $className,
									teacherName: $teacherName
								});
							}
						}
					} else {
						this.$util.errorMsg(response.data.message);
					}
				});
			},
			//导出数据
			getImportUrl() {
				let url = this.$util.domain + "/campusadminapi/v1/cheat/export";

				this.$http({
					url,
					method: "POST",
					body: JSON.stringify({
						sid: parseInt(this.semester),
						mid: parseInt(this.teacher),
						cid: parseInt(this.classes)
					})
				}).then(response => {
					if (response.data.error == 10000) {
						let importUrl = response.data.data.exportUrl;
						location.href = importUrl;
					}
				});
			},
			//请求分页数据分页
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getResult();
			},
			//详情
			toDetail(row) {
				let params = {
					cheatId: row.cheatId,
					teacher: this.teacher,
					classes: this.classes,
					semester: this.semester,
					pageNum: this.currentPage,
					condition: encodeURI(this.condition.trimLeft())
				};
				this.$util.refreshTo("/#/cheatDetail", params);
			},
			//获取功能按钮状态；           
			getActionStatus() {
				let url = this.$util.domain + "/campusadminapi/v1/cheat/getOpenState";

				this.$http({
					url,
					method: "GET"
				}).then(response => {
					if (response.data.error == 10000) {
						this.isOpen = response.data.data == 0 ? true : false;
					}
				});

			},
			//进行作弊功能的开启和关闭；openCheat(是否开启 0-是 1-否)
			///campusadminapi/v1/cheat/openState/{openCheat} 更新开启状态接口
			operat(status) {
				let msg, title, isOpen;
				if (status == 0) {
					msg = "开启后，学生端 将显示作弊黑榜数据；";
					title = "开启作弊黑榜功能";
					isOpen = true;
				} else {
					msg = "关闭后，学生端将不显示作弊数据；如需继续使用，请再开启功能，数据保留；";
					title = "关闭作弊黑榜功能";
					isOpen = false;
				}

				this.$confirm(msg, title, {
					confirmButtonText: "确定",
					cancelButtonText: "取消"
				}).then(() => {
					//确定
					let url = this.$util.domain + `/campusadminapi/v1/cheat/openState/${status}`;
					this.$http({
						url,
						method: "GET"
					}).then(response => {
						if (response.data.error == 10000) {
							this.isOpen = isOpen;
							this.$util.successMsg("操作成功");
						}
					});
				}).catch(() => {
					//取消
				});
			}
		}
	};
</script>

<style>
	#cheat-manage {
		padding-top: 60px;
		height: calc(100% - 100px);
	}

	#cheat-manage .el-table--enable-row-hover .el-table__body tr:hover>td {
		background-color: #f7fcff;
		cursor: pointer;
	}

	#cheat-manage .search {
		margin: 20px 30px;
		background: #ffffff;
		border-radius: 2px;
		/* height: 40px; */
		padding: 25px;
		position: relative;
	}

	#cheat-manage .import1 {
		position: absolute;
		right: 0px;
	}

	#cheat-manage .sel_btn {
		position: absolute;
		right: 25px;
		top: 13px;
		margin-top: 12px;
	}

	#cheat-manage .el-select {
		display: inline-block;
		width: 20%;
	}

	#cheat-manage .time {
		display: inline-block;
		width: 15%;
		min-width: 150px;
	}

	#cheat-manage .inp {
		display: inline-block;
		width: 350px;
	}

	#cheat-manage .fr {
		float: right;
		margin-right: 265px;
	}
</style>